<?php

namespace App\Api\V1\Controllers;

use App\Http\Controllers\Controller;
use Dingo\Api\Http\Request;
use Illuminate\Http\JsonResponse;

class BaseController extends Controller
{
    /**
     * Request实例
     * @var Request
     */
    protected $request;

    /**
     * 应用实例
     * @var App
     */
    protected $app;

    /**
     * 控制器错误信息
     * @var string
     */
    public $error;

    /**
     * AppKey
     * @var string
     */
    public $appkey;

    /**
     * AppSecret
     * @var string
     */
    public $appSecret;

    /**
     * Token
     * @var string
     */
    public $token;

    /**
     * Sign
     * @var string
     */
    public $sign;

    /**
     * 时间戳
     * @var int
     */
    public $timestamp;

    /**
     * 返回格式
     * @var string
     */
    public $format;

    /**
     * 业务方法
     * @var string
     */
    public $method;

    /**
     * 业务参数
     * @var array
     */
    public $params = [];

    /**
     * 方法路由器
     * @var array
     */
    protected static $route = [];

    /**
     * 对应模型
     * @var object
     */
    protected static $model;

    /**
     * 权限验证实例
     * @var object
     */
    protected static $auth;

    /**
     * 是否调试模式
     * @var bool
     */
    public $apiDebug = false;

    /**
     * 构造方法
     * @access public
     * @throws
     */
    public function __construct(Request $request)
    {

        $this->request = $request;

        // 获取外部参数
        $this->params = $this->request->input();

        // 获取方法路由器
        static::initMethod();

    }

    protected static function initMethod()
    {

    }

    /**
     * 核心基础首页
     * @access public
     * @return array
     */

    public function index($method)
    {
        $this->method = $method;
        $result = null;
        $callback = isset(self::$route[$this->method]) ? self::$route[$this->method] : null;

        $action = $this->request->route()->getAction()['controller'];
        list($class, $method) = explode('@', $action);
        // 路由定义中如果数组[1]不存在,则表示默认对应model模型
        if (!isset($callback[1])) {
            $className = str_replace('Controller', '', $class);
            $callback[1] = 'App\\Api\\V1\\Models\\' . $className;
        }

        if (class_exists($callback[1])) {
            isset(static::$model) ?: static::$model = new $callback[1];
        }else{
            return $this->outputError('模型类或方法不存在');
        }

        try {
            if (method_exists($this, $callback[0])) {
                $result = call_user_func_array([$this, $callback[0]], []);
            } else if (method_exists(static::$model, $callback[0])) {
                $result = call_user_func([static::$model, $callback[0]], $this->getParams());
            } else {
                return $this->outputError('method成员方法不存在');
            }
        } catch (Exception $e) {
            $result = false;
        }

        // 记录日志


        // 输出结果
        return $this->outputResult($result);
    }

    /**
     * 获取公共参数
     * @access protected
     * @param null $key
     * @return mixed
     */
    protected function getParams($key = null)
    {
        return is_null($key) ? $this->params : $this->params[$key];
    }

    /**
     * api_debug模式下,尝试根据method命名规范查找模型方法(方便调试)
     * @access private
     * @return void
     */
    private function autoFindMethod()
    {

    }

    /**
     * 输出请求结果
     * @access protected
     * @param array $data 业务结果
     * @param int   $code HTTP状态码
     * @return array
     */
    protected function outputResult($data = [], $code = 200, $message = '成功')
    {
        return new JsonResponse([
            'code' => 200,
            'message' => $message,
            'data' => $data
        ]);
    }

    /**
     * 输出错误结果
     * @access protected
     * @param string $message 错误消息
     * @param int    $code    错误编码
     * @return void
     */
    protected function outputError($message = '', $code = 500)
    {
        return new JsonResponse([
            'code' => $code,
            'message' => $message,
        ]);
    }


}
